package com.hsnn.medstgmini.filter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.sys.model.SysUser;

public class ExpertAuthFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		HttpSession session = httpRequest.getSession(true);
		String uri = httpRequest.getRequestURI();
		SysUser sysUser = (SysUser) session.getAttribute(Constants.USERINFO);
		String ctx = session.getServletContext().getContextPath();
		if(null == sysUser){
			chain.doFilter(httpRequest, httpResponse);
			return;
		}
		if(null!= sysUser.getType()&&null!=sysUser.getPasswordExpired()){
			if(sysUser.getType()!=5){
				chain.doFilter(httpRequest, httpResponse);
				return;
			}
			if(uri.contains("ExpertChangePassword")||uri.contains("expertPasswordChange")){
				chain.doFilter(httpRequest, httpResponse);
				return;
			}
			if(sysUser.getPasswordExpired().after(new Date())){
				chain.doFilter(httpRequest, httpResponse);
				return;
			}else{
				httpResponse.sendRedirect(ctx+"/toExpertChangePassword.html");
				return;
			}
		}
		chain.doFilter(httpRequest, httpResponse);
		return;
	}
	@Override
	public void destroy() {
		
	}
	
}
